package com.sprd.simple.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sprd.simple.launcher.Launcher;
import com.sprd.simple.launcher.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.widget.GridView.AUTO_FIT;

/**
 * Created by SPRD on 2016/7/19.
 */
public class FamilyAdapter extends BaseListViewAdapter<HashMap<String, Object>> {
    public static final String TAG = "FamilyAdapter";
    public static final String DATABASE = "FamilyNumber";
    private Animation mIconAnimation;
    private GridView mGridView;

    public FamilyAdapter(Context context, List<HashMap<String, Object>> menuList, GridView gridView) {
        super(context, menuList);
        mGridView = gridView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int count = mGridView.getCount();
        int numColumns = 0;
        if (Build.VERSION.SDK_INT >= 11) {
            numColumns =  mGridView.getNumColumns();

        } else {
            int columns = 0;
            int children = mGridView.getChildCount();
            if (children > 0) {
                int width = mGridView.getChildAt(0).getMeasuredWidth();
                if (width > 0) {
                    columns = mGridView.getWidth() / width;
                }
            }
            numColumns = columns > 0 ? columns : AUTO_FIT;
        }
        if (numColumns == AUTO_FIT) {
            numColumns = 1;
        }
        ItemViewTag viewTag;
        float marginSize = mContext.getResources().getDimension(R.dimen.layout_margin_size);
        float horizontalSpacing = mContext.getResources().getDimension(R.dimen.horizontalSpacing_grid_view);
        int gridHeight = mGridView.getHeight();
        int row = count / numColumns;
        int itemHeight = (int) ((gridHeight - horizontalSpacing * (row - 1)) / (row));
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.family_item, null);
            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, itemHeight
            ));
            viewTag = new ItemViewTag((ImageView) convertView.findViewById(R.id.family_item_image),
                    (TextView) convertView.findViewById(R.id.family_item_name));
            convertView.setTag(viewTag);
        } else {
            viewTag = (ItemViewTag) convertView.getTag();
            ViewGroup.LayoutParams param = convertView.getLayoutParams();
            param.width = ViewGroup.LayoutParams.MATCH_PARENT;
            param.height = itemHeight;
            convertView.setLayoutParams(param);
        }
        SharedPreferences sp = mContext.getSharedPreferences(DATABASE, Context.MODE_PRIVATE);
        if (sp.contains(position + "") == true) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            viewTag.itemName.setLayoutParams(layoutParams);
            viewTag.itemName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mContext.getResources().getDimension(R.dimen.family_item_textsize));
        } else {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            viewTag.itemName.setLayoutParams(layoutParams);
            viewTag.itemName.setTextSize(TypedValue.COMPLEX_UNIT_DIP,mContext.getResources().getDimension(R.dimen.family_item_textsize_empty));
        }
        viewTag.itemImage.setBackgroundResource((Integer) mInfos.get(position).get("ItemImage"));
        viewTag.itemName.setText(mInfos.get(position).get("ItemName").toString());
        Log.d(TAG, "Name = " + mInfos.get(position).get("ItemName").toString() + " position = " + position);

        // add the animation for icon
        if ((mPosition == position) && (Launcher.animAble)) {
            mIconAnimation = AnimationUtils.loadAnimation(mContext, R.anim.icon_anim);
            mIconAnimation.setRepeatMode(Animation.REVERSE);
            convertView.setAnimation(mIconAnimation);
            mIconAnimation.startNow();
            Log.d(TAG, "startAnimation");
        } else {
            convertView.clearAnimation();
        }

        return convertView;

    }

    public void setSelectPosition(int position) {
        setPosition(position);
    }

	//add by wpf20170104 start SPCSS00348039
	public int getSelectPosition() {
        return mPosition;
    }
	//add by wpf20170104 end

    class ItemViewTag {
        protected ImageView itemImage;
        protected TextView itemName;

        public ItemViewTag(ImageView imageView, TextView name) {
            this.itemImage = imageView;
            this.itemName = name;
        }
    }
}
